package com.yyryyty.chapter6;

import com.yyryyty.chapter4.Dish;

import java.util.IntSummaryStatistics;

import static com.yyryyty.chapter4.Dish.menu;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.summarizingInt;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-01-18 07:20
 */
public class Summarizing {

    public static void main(String[] args) {

//        System.out.println(menu.stream()
//                .collect(counting()));
//
//        System.out.println(menu.stream().count());
//
//        Optional<Dish> result = menu.stream()
//                .max(Comparator.comparingInt(Dish::getCalories));
//
//        result.ifPresent(dish -> System.out.println(dish.getName()));

//        System.out.println((Integer) menu.stream().mapToInt(Dish::getCalories).sum());

//        System.out.println(menu.stream()
//                .mapToInt(Dish::getCalories)
//                .sum());
//
//        System.out.println(menu.stream()
//                .collect(averagingInt(Dish::getCalories)));

//        menu.stream()
//                .mapToInt(Dish::getCalories)
//                .average()
//                .ifPresent(System.out::println);

        IntSummaryStatistics resultForAll = menu.stream()
                .collect(summarizingInt(Dish::getCalories));

//        System.out.println(resultForAll.getAverage());
//        System.out.println(resultForAll.getCount());
//        System.out.println(resultForAll.getMax());
//        System.out.println(resultForAll.getMin());
//        System.out.println(resultForAll.getSum());

        System.out.println(menu.stream()
                .map(Dish::getName)
                .collect(joining()));

        System.out.println(menu.stream()
                .map(Dish::getName)
                .collect(joining(",")));

        String namesUnTrim = menu.stream()
                .map(Dish::getName)
                .reduce("", (name1, name2) -> name1 + "," + name2);
        System.out.println(namesUnTrim.substring(1));

    }
}
